import java.util.*;

public class Example {
    public static void main(String args[]) {
        ArrayList<String> wordBank = new ArrayList<>();
        ArrayList<Integer> guessedWordIndices = new ArrayList<>();
        Random r = new Random();
        Scanner in = new Scanner(System.in);
        fillWordBank(wordBank);

        //游戏程序
        while (guessedWordIndices.size() < wordBank.size()) {
            ArrayList<String> guessedLetters = new ArrayList<>();
            int leftTryTimes = 6;
            boolean right = true;
            //选中一个单词
            int wordIndex = r.nextInt(wordBank.size());
            while (guessedWordIndices.contains(wordIndex)) {
                wordIndex = r.nextInt(wordBank.size());
            }
            guessedWordIndices.add(wordIndex);
            //初始化输出字符串
            String word = wordBank.get(wordIndex);
            StringBuilder output = new StringBuilder();
            for (int i = 1; i <= word.length(); i++) output.append("*");
            System.out.println("现在我的脑中有一个单词，试着猜猜看吧！");
            System.out.println("加密的单词将显示在下面，请尝试猜测单词中有什么字母，并在\">\"后输入此字母。你最多有六次错误的机会。");
            while (hasChar(output, '*')) {
                System.out.print("单词：" + output + " > ");
                //获取输入
                String input = in.nextLine().substring(0, 1);
                if (guessedLetters.contains(input)) {
                    System.out.println("\t这个字母你已经猜过啦！");
                    continue;
                }
                guessedLetters.add(input);
                //猜对字母，更改输出使得改字母得以显示
                if (hasChar(word, input.charAt(0))) {
                    replaceAll(word, output, input);
                }
                //猜错，输出错误信息
                else {
                    if (leftTryTimes == 0) {
                        right = false;
                        break;
                    }
                    else {
                        leftTryTimes--;
                        if (leftTryTimes > 0)
                            System.out.println("\t单词不包含此字母。你还有" + leftTryTimes + "次错误的机会。");
                        else System.out.println("\t单词不包含此字母。下次输错，本局游戏就将失败。");
                    }
                }
            }
            //一个单词结束，根据是否猜出输出提示
            System.out.println("单词为：" + word);
            if (right) System.out.println("猜对啦！您可真是个天才！");
            else System.out.println("你没猜对，真不走运！下次再试试运气吧！");
            System.out.print("还想再来一局吗？ Y or N > ");
            String choice = in.nextLine().substring(0, 1);
            if (choice.equals("N") || choice.equals("n")) {
                System.out.println("期待下次再见！");
                return;
            }
        }
        //所有的词都已被猜过，输出提示
        System.out.println("看起来所有的词都被您猜过了，敬请期待作者的下一部作品！");
        System.out.println("期待下次再见！");
    }

    private static void fillWordBank(ArrayList<String> wordBank) {
        wordBank.add("freeman");
        wordBank.add("serena");
        wordBank.add("advantage");
        wordBank.add("appetite");
        wordBank.add("bottom");
        wordBank.add("charge");
        wordBank.add("demonstrate");
        wordBank.add("embarrass");
        wordBank.add("forward");
        wordBank.add("genius");
        wordBank.add("however");
        wordBank.add("infinity");
        wordBank.add("justice");
        wordBank.add("knowledge");
        wordBank.add("language");
        wordBank.add("member");
        wordBank.add("nuclear");
        wordBank.add("obsess");
        wordBank.add("ocean");
        wordBank.add("photography");
        wordBank.add("quantity");
        wordBank.add("rain");
        wordBank.add("response");
        wordBank.add("silence");
        wordBank.add("star");
        wordBank.add("terrify");
        wordBank.add("untiy");
        wordBank.add("verify");
        wordBank.add("wardrobe");
        wordBank.add("yesterday");
        wordBank.add("zero");
    }

    private static boolean hasChar(StringBuilder str, char ch) {
        for (int i = 0; i <= str.length() - 1; i++) {
            if (str.charAt(i) == ch) return true;
        }
        return false;
    }

    private static boolean hasChar(String str, char ch) {
        for (int i = 0; i <= str.length() - 1; i++) {
            if (str.charAt(i) == ch) return true;
        }
        return false;
    }

    //replaceAll函数逐个检查srcStr的字符，如发现ch，则将tarStr对应位置的字符替换为ch
    private static void replaceAll(String srcStr, StringBuilder tarStr, String ch) {
        for (int i = 0; i <= srcStr.length() - 1; i++) {
            if (srcStr.substring(i, i + 1).equals(ch)) tarStr.replace(i, i + 1, ch);
        }
    }
}